<?php
// +----------------------------------------------------------------------
// | ZGUAN [ 致力于帮助更多的中小企业物联网转型 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2024 https://www.zguan.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ZGUAN 并不是自由软件，未经许可不能去掉ZGUAN 相关版权
// +----------------------------------------------------------------------
// | Author: ZGUAN Team <admin@zguan.cn>  0769-82282996  15899929162
// +----------------------------------------------------------------------
namespace app\common\model\property\basic;

use app\helper\Time;
use app\common\model\BaseModel;
use app\common\model\room\RoomBasicModel;
use app\common\model\system\SystemRegionModel;
use app\common\model\firm\basic\FirmBasicModel;
use app\common\model\contract\ContractBasicModel;
use app\common\model\system\upload\SystemUploadFileModel;

class PropertyBasicModel extends BaseModel
{
    protected $autoWriteTimestamp = 'datetime';
    protected $createTime = 'add_time';
    protected $updateTime = false;

    public static function tablePk(): string
    {
        return 'id';
    }

    public static function tableName(): string
    {
        return 'property_basic';
    }

    public function cateInfo()
    {
        return $this->hasOne(PropertyCateModel::class, 'id', 'cate_id')->field('id,name');
    }

    public function fileInfo()
    {
        return $this->hasOne(SystemUploadFileModel::class, 'id', 'picture_id');
    }

    public function getLabelIdsAttr()
    {
        return PropertyBasicLabelModel::where('property_id', $this->id)->column('label_id');
    }


    public function getLabelListAttr()
    {
        if ($this->LabelIds) {
            return PropertyLabelModel::whereIn('id', $this->LabelIds)->field('id,label_name')->select()->toArray();
        }
        return [];
    }

    /**
     * 房间总数量
     * @return string
     *
     * @author Amos
     * 2024-04-21 18:19
     */
    public function getRoomAllAttr()
    {
        return RoomBasicModel::where('property_id', $this->id)->count('id');
    }

    /**
     * 空置房间总数量
     * @return string
     *
     * @author Amos
     * 2024-04-21 18:19
     */
    public function getRoomEmptyAllAttr()
    {
        return RoomBasicModel::where('property_id', $this->id)
            ->where('status', '1')
            ->count('id');
    }

    /**
     * 今日入住数量
     * @return string
     *
     * @author Amos
     * 2024-04-21 18:23
     */
    public function getTodayCheckInAttr()
    {
        return ContractBasicModel::where('property_id', $this->id)
            ->where('add_time', 'between', Time::today(true))
            ->count('id');
    }

    /**
     * 今日退租数量
     * @return string
     *
     * @author Amos
     * 2024-04-21 18:23
     */
    public function getTodayRefundAttr()
    {
        return ContractBasicModel::where('property_id', $this->id)
            ->where('end_date', 'between', Time::today(true))
            ->count('id');
    }


    /**
     * 有效入住数量
     * @return string
     *
     * @author Amos
     * 2024-04-21 18:23
     */
    public function getAllCheckInAttr()
    {
        return ContractBasicModel::where('property_id', $this->id)
            ->where('status', '2')
            ->count('id');
    }


    /**
     * 空置率
     * @return string
     *
     * @author Amos
     * 2024-04-21 18:23
     */
    public function getRoomVacancyRateAttr()
    {
        # 空置房间数量 =  （房间总数量 / 有效入住数量）* 100
        if ($this->room_empty_all > 0 && $this->room_all > 0) {
            return bcmul(bcdiv($this->room_empty_all, $this->room_all, 4), 100, 2);
        }
        return '0.00';
    }

    /**
     * 省
     * @return string
     *
     * @author Amos
     * 2024-06-13 18:20
     */
    public function getProvinceNameAttr()
    {
        if ($this->province) {
            return SystemRegionModel::where('id', $this->province)->value('name');
        }
        return '';
    }

    /**
     * 市
     * @return string
     *
     * @author Amos
     * 2024-06-13 18:20
     */
    public function getCityNameAttr()
    {
        if ($this->city) {
            return SystemRegionModel::where('id', $this->city)->value('name');
        }
        return '';
    }

    /**
     * 县
     * @return string
     *
     * @author Amos
     * 2024-06-13 18:20
     */
    public function getDistrictNameAttr()
    {
        if ($this->district) {
            return SystemRegionModel::where('id', $this->district)->value('name');
        }
        return '';
    }

}
